package com.elegant.code.spi.dao.mybatis;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;


/**
 * Created by dulei on 10/11/17.
 */
@Slf4j
@Aspect
public class MybatisAuditAOPDefault {
    @Pointcut("execution(* com.elegant.*.dao..*.*Mapper.insert*(..))")
    private void insertCutMethod() {
    }

    @Pointcut("execution(* com.elegant.*.repository..*.*Mapper.update*(..))")
    private void updateCutMethod() {
    }

    @Around("insertCutMethod()")
    public Object doInsertAround(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        for (Object arg : args) {
            AuditingAction.markCreateAuditing(arg);
        }
        return pjp.proceed();
    }

    @Around("updateCutMethod()")
    public Object doupdateAround(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        for (Object arg : args) {
            AuditingAction.markUpdateAuditing(arg);
        }
        return pjp.proceed();
    }
}